$(function (){
   //加载弹出层
   layui.use(['form','element'],
    function() {
        layer = layui.layer;
        element = layui.element;
    });

   $(".layui-tab-title li").bind('click',function(){
        console.log(22);
   })

  //触发事件
  var tab = {
        tabAdd: function(title,url,id){

          //新增一个Tab项
          element.tabAdd('xbs_tab', {
            title: title
            ,content: '<iframe  tab-id="'+id+'" frameborder="0" src="'+url+'" scrolling="yes" class="x-iframe"></iframe>'
            ,id: id
          })
        }
        ,tabDelete: function(othis){
          //删除指定Tab项
          element.tabDelete('xbs_tab', '44'); //删除：“商品管理”
          othis.addClass('layui-btn-disabled');
        }
        ,tabChange: function(id){
          //切换到指定Tab项
          element.tabChange('xbs_tab', id); //切换到：用户管理
        }
      };



    tableCheck = {
        init:function  () {
            $(".layui-form-checkbox").click(function(event) {
                if($(this).hasClass('layui-form-checked')){
                    $(this).removeClass('layui-form-checked');
                    if($(this).hasClass('header')){
                        $(".layui-form-checkbox").removeClass('layui-form-checked');
                    }
                }else{
                    $(this).addClass('layui-form-checked');
                    if($(this).hasClass('header')){
                        $(".layui-form-checkbox").addClass('layui-form-checked');
                    }
                }

            });
        },
        getData:function  () {
            var obj = $(".layui-form-checked").not('.header');
            var arr=[];
            obj.each(function(index, el) {
                arr.push(obj.eq(index).attr('data-id'));
            });
            return arr;
        }
    }

    //开启表格多选
    tableCheck.init();

    $('.container .left_open i').click(function(event) {
        if($('.left-nav').css('left')=='0px'){
            $('.left-nav').animate({left: '-221px'}, 100);
            $('.page-content').animate({left: '0px'}, 100);
            $('.page-content-bg').hide();
        }else{
            $('.left-nav').animate({left: '0px'}, 100);
            $('.page-content').animate({left: '221px'}, 100);
            if($(window).width()<768){
                $('.page-content-bg').show();
            }
        }

    });

    $('.page-content-bg').click(function(event) {
        $('.left-nav').animate({left: '-221px'}, 100);
        $('.page-content').animate({left: '0px'}, 100);
        $(this).hide();
    });



    $('.layui-tab-close').click(function(event) {
        $('.layui-tab-title li').eq(0).find('i').remove();
    });

    //左侧菜单效果
    // $('#content').bind("click",function(event){
    $('.left-nav #nav li').click(function (event) {
        if($(this).children('.sub-menu').length){
            if($(this).hasClass('open')){
                $(this).removeClass('open');
                $(this).find('.nav_right').html('&#xe697;');
                $(this).children('.sub-menu').stop().slideUp();
                // $(this).siblings().children('.sub-menu').slideUp();
            }else{
                $(this).addClass('open');
                $(this).children('a').find('.nav_right').html('&#xe6a6;');
                $(this).children('.sub-menu').stop().slideDown();
                // $(this).siblings().children('.sub-menu').stop().slideUp();
                // $(this).siblings().find('.nav_right').html('&#xe697;');
                // $(this).siblings().removeClass('open');
            }
        }else{

            var url = $(this).children('a').attr('_href');
            var title = $(this).find('cite').html();
            var index  = $('.left-nav #nav li').index($(this));

            for (var i = 0; i <$('.x-iframe').length; i++) {
                if($('.x-iframe').eq(i).attr('tab-id')==index+1){
                    tab.tabChange(index+1);
                    event.stopPropagation();
                    return;
                }
            };

            tab.tabAdd(title,url,index+1);
            tab.tabChange(index+1);
        }

        event.stopPropagation();

    })

})

/*弹出层*/
/*
    参数解释：
    title   标题
    url     请求的url
    id      需要操作的数据id
    w       弹出层宽度（缺省调默认值）
    h       弹出层高度（缺省调默认值）
*/
function x_admin_show(title,url,w,h,offset){
    if (offset == null || offset == '') {
        offset = 30;
    }
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = '90%';
    } else {
        w = w + 'px';
    }
    if (h == null || h == '') {
        h = '90%';
    }else{
        h = h + 'px';
    }
    layer.open({
        // offset:offset +'px', //右下角弹出
        type: 2,
        area: [w, h],
        fix: false, //不固定
        maxmin: true,
        shadeClose: false,//是否点击遮罩关闭弹窗
        shade:0.2,
        resize:true,
        title: title,
        content: url
    });
}
/**
 * 图片放大
 * <img src="" onclick="previewImg(this)">
 * @param obj
 */
function previewImg(obj) {
    var img = new Image();
    img.src = obj.src;
    var imgHtml = "<img src='" + obj.src + "' style='width: 500px !important;height: 500px !important;'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: ['500px','500px'],
        shadeClose:true,
        scrollbar: false,
        closeBtn: 0,
        title: false, //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}
/**
 * 图片放大
 * <a href="javascript:void(0);" onclick="previewImgUrl('{$url}')">{$vo.name}</a>
 * @param obj
 */
function previewImgUrl(url) {
    var imgHtml = "<img src='" + url + "' style='width: 500px !important;height: 500px !important;'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: ['500px','500px'],
        shadeClose:true,
        scrollbar: false,
        closeBtn: 0,
        title: false, //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}
var adminindex = -1;
function openinfo() {

    if(adminindex != -1 ){
        return false;
    }
    adminindex = layer.open({
        type: 2,
        area: ['500px', '520px'],
        fix: false, //不固定
        maxmin: true,
        shadeClose: false,//是否点击遮罩关闭弹窗
        shade:0.2,
        resize:true,
        title: '个人信息',
        content: '/admin/user/info',
        end:function(){adminindex=-1},
        cancel:function(){adminindex=-1}
    });
}

function p_admin_show(title,url,w,h,offset){
    if (offset == null || offset == '') {
        offset = 30;
    }
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = '90%';
    } else {
        w = w + 'px';
    }
    if (h == null || h == '') {
        h = '90%';
    }else{
        h = h + 'px';
    }
    parent.layer.open({
        // offset:offset +'px', //右下角弹出
        type: 2,
        area: [w, h],
        fix: false, //不固定
        maxmin: true,
        shadeClose: false,//是否点击遮罩关闭弹窗
        shade:0.2,
        resize:true,
        title: title,
        content: url
    });
}

function admin_show(title,url,w,h,offset){
    if (offset == null || offset == '') {
        offset = 30;
    }
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = '90%';
    } else {
        w = w + 'px';
    }
    if (h == null || h == '') {
        h = '90%';
    }else{
        h = h + 'px';
    }
    parent.layer.open({
        type: 2,
        area: [w, h],
        fix: false, //不固定
        maxmin: true,
        shadeClose: false,//是否点击遮罩关闭弹窗
        shade: 0.2,
        resize:true,
        title: title,
        content: url
    });
}
function show_full(title,url){
    var index = layer.open({
        type: 2,
        content: url,
        title: title,
        shade:0.2,
        shadeClose: true,
        resize:true,
        area: ['100%', '100%'],
        maxmin: false
    });
    layer.full(index);
}


/*关闭弹出框口*/
function x_admin_close(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
function m_admin_show(title,url,w,h,offset){
    if (offset == null || offset == '') {
        offset = 30;
    }
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = '90%';
    }
    if (h == null || h == '') {
        h = '90%';
    }
    layer.open({
        // offset:offset +'px', //右下角弹出
        type: 2,
        area: [w, h],
        fix: false, //不固定
        maxmin: true,
        resize:true,
        shadeClose: false,//是否点击遮罩关闭弹窗
        shade:0.2,
        title: title,
        content: url
    });
}

/**
 * 自刷新弹窗
 * @param title
 * @param url
 * @param w
 * @param h
 */
function r_admin_show(title,url,w,h,offset){
    if (offset == null || offset == '') {
        offset = 30;
    }
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = '90%';
    } else {
        w = w + 'px';
    }
    if (h == null || h == '') {
        h = '90%';
    }else{
        h = h + 'px';
    }
    layer.open({
        // offset:offset +'px', //右下角弹出
        type: 2,
        area: [w, h],
        fix: false, //不固定
        maxmin: true,
        shadeClose: false,//是否点击遮罩关闭弹窗
        shade:0.2,
        resize:true,
        title: title,
        content: url,
        // yes: function (index, obj) {
        //     location.reload();
        // }
    });
}








